<template>
<div  style="width: 80%">
  <div style="margin-bottom:30px ">新增题目</div>
  <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
    <el-form-item label="所属科目" prop="name">
      <el-input v-model="form.name" placeholder="请输入所属科目"></el-input>
    </el-form-item>

    <el-form-item label="难度系数" prop="grade">
      <el-input v-model="form.grade" placeholder="请输入难度系数"></el-input>
    </el-form-item>
    <el-form-item label="题目类型">
      <el-input v-model="form.type" placeholder="请输入题目类型"></el-input>
    </el-form-item>
    <el-form-item label="题目">
      <el-input v-model="form.topic"  type="textarea" placeholder="请输入题目"></el-input>
    </el-form-item>
    <el-form-item label="答案">
      <el-input v-model="form.answer"   type="textarea" placeholder="请输入答案"></el-input>
    </el-form-item>

    </el-form>
  <div style="text-align: center;margin-top: 30px">
<el-button type="primary" @click="save" size="medium">提交</el-button>
 <!--<el-button type="danger">返回列表</el-button>-->

  </div>

</div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "AddUser",
  data(){
   const checkAge = (rule, value, callback) => {
     if (!value) {
       return callback(new Error('难度系数不能为空'));
     }

       if (!/^[0-9]+$/.test(value)){
         callback(new Error('请输入数字值'));
       }
       if(parseInt(value)>5 || parseInt(value) <=0){
         callback(new Error("请输入合理的难度系数"))
       }

         callback()


   };
    return{
      form:{},
      rules: {
        name: [
          {required: true, message: '请输入所属科目', trigger: 'blur'},

        ],
        grade: [
          {validator: checkAge, trigger: 'blur'}
        ]
      }


      }

        },
  methods: {
    save() {
      this.$refs['ruleForm'].validate((valid) => {
if (valid){
  request.post('/question/save', this.form).then(res => {
    if (res.code === '200') {
      this.$notify.success('新增成功')
      this.$refs['ruleForm'].resetFields()

    } else {
      this.$notify.error(res.msg)


}
  })
      }
    })
  }
}
}
</script>

<style scoped>

</style>